//
//  LPSanHuiYiKeDetailViewController.m
//  GongXinDangJian
//
//  Created by 刘应平 on 2017/11/30.
//  Copyright © 2017年 apple. All rights reserved.
//

#import "LPSanHuiYiKeDetailViewController.h"
#import "LPSanHuiYiKeTypeDetailListTableViewCell.h"
#import "LPSanHuiMeetingListModel.h"
#import "LPSanHuiYKSaveRecordViewController.h"
#import "LPSanHuiYKListDetailViewController.h"
#define CELLID @"LPSanHuiYiKeDetailViewController"
@interface LPSanHuiYiKeDetailViewController ()<UITableViewDelegate,UITableViewDataSource>
/**  table  */
@property (strong, nonatomic) UITableView  *tableView;
/**  数据  */
@property (strong, nonatomic) NSMutableArray  *dataSouce;

/**  页数  */
@property (assign, nonatomic) NSInteger  page;
/** 滑动 */
@property (nonatomic, strong) ZJRefurbish *refurbish;
/** 类型值  */
@property (strong, nonatomic) NSString  *typyString;

/**  添加按钮  */
@property (strong, nonatomic) UIButton  *addButton;


@end

@implementation LPSanHuiYiKeDetailViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.titlelabel.text = self.headLine;
    [self getType];
    __weak typeof(self) weakSelf = self;
    [self.refurbish headerToScollview:self.tableView Block:^{
        weakSelf.page = 1;
        [weakSelf netWorking_header];

    }];
    [self.refurbish footerToScollview:self.tableView Block:^{
        weakSelf.page ++;
        [weakSelf netWorking_footer];
    }];
    [self.tableView.mj_header beginRefreshing];
    [self layoutUI];
}

- (void)getType{
    if ([self.headLine isEqualToString:@"支部党员大会"]) {
        self.typyString = @"0";
    }else if ([self.headLine isEqualToString:@"党课"]){
         self.typyString = @"1";
    }else if ([self.headLine isEqualToString:@"党支部委员会"]){
         self.typyString = @"2";
    }else if ([self.headLine isEqualToString:@"党小组会议"]){
         self.typyString = @"3";
    }else if ([self.headLine isEqualToString:@"记录"]){
         self.typyString = @"4";
    }
}
#pragma mark - 数据
//下拉刷新
- (void)netWorking_header{
    __weak typeof(self) weakSelf = self;
    NSString *page = [NSString stringWithFormat:@"%ld",self.page];
    [LPRequestManager request_miitactionShykActiongetShykListWithTYPE_VALUE:self.typyString PAGE_INDEX:page  successBlock:^(id object) {
        NetWorkModel *model = [NetWorkModel yy_modelWithJSON:object];
        if ([model.STATE integerValue] == 20000) {
            NSArray *array = [object objectForKey:@"SHYKS"];
            weakSelf.dataSouce = [[NSArray yy_modelArrayWithClass:[LPSanHuiMeetingListModel class] json:array] mutableCopy];
            [weakSelf.tableView reloadData];
        }else{
            [YJProgressHUD showSuccess:@"数据加载失败" inview:[UIApplication sharedApplication].keyWindow];
        }
        [weakSelf.tableView.mj_header endRefreshing];
    } failBlock:^(id object) {
        [weakSelf.tableView.mj_header endRefreshing];
        [YJProgressHUD showSuccess:@"网络错误" inview:[UIApplication sharedApplication].keyWindow];
    }];
    
    
    
   
    
}

//上拉加载
- (void)netWorking_footer{
    __weak typeof(self) weakSelf = self;
     NSString *page = [NSString stringWithFormat:@"%ld",self.page];
    [LPRequestManager request_miitactionShykActiongetShykListWithTYPE_VALUE:self.typyString PAGE_INDEX:page successBlock:^(id object) {
        NetWorkModel *model = [NetWorkModel yy_modelWithJSON:object];
        if ([model.STATE integerValue] == 20000) {
            NSArray *array = [object objectForKey:@"SHYKS"];
            if (array.count == 0) {
                _page --;
            }else{
               [ weakSelf.dataSouce addObjectsFromArray:[NSArray yy_modelArrayWithClass:[LPSanHuiMeetingListModel class] json:array]];
                [weakSelf.tableView reloadData];
            }
            
        }
        [weakSelf.tableView.mj_footer endRefreshing];
    } failBlock:^(id object) {
        [weakSelf.tableView.mj_footer endRefreshing];
    }];
    
    
    
}

- (void)clickButton{
    __weak typeof(self) weakSelf = self;
    LPSanHuiYKSaveRecordViewController *vc = [LPSanHuiYKSaveRecordViewController new];
    vc.headline = self.headLine;
    vc.type = self.typyString;
    vc.block = ^{
        [weakSelf.tableView.mj_header beginRefreshing];
    };
    [self.navigationController pushViewController:vc animated:YES];
}

#pragma mark - 界面
- (void)layoutUI{
    [self.view addSubview:self.tableView];
    TenGrayShadowView *five = [[TenGrayShadowView alloc] initWithFrame:CGRectMake(0, 0, [UIScreen zj_screenWith], 5 * [UIScreen zj_screenHScale])];
    self.tableView.tableHeaderView = five;
    
    [self.view addSubview:self.addButton];
    self.addButton.sd_layout
    .rightSpaceToView(self.view, 20 * [UIScreen zj_screenWScale])
    .heightIs(70 * [UIScreen zj_screenHScale])
    .widthIs(70 * [UIScreen zj_screenHScale])
    .bottomSpaceToView(self.view, 30 * [UIScreen zj_screenHScale]);
}



#pragma mark - UITableViewDataSource,UITableViewDelegate
/**  返回组个数  */
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}
/**  返回cell个数  */
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.dataSouce.count;
}
/**  返回cell高度  */
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    LPSanHuiMeetingListModel *model = self.dataSouce[indexPath.row];
    return model.CELLHEIGHT;
}
/**  cell初始化  */
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
   
    LPSanHuiYiKeTypeDetailListTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CELLID forIndexPath:indexPath];
    cell.model = self.dataSouce[indexPath.row];
    return cell;
}


/**  cell的点击事件  */
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    LPSanHuiYKListDetailViewController *vc = [LPSanHuiYKListDetailViewController new];
    vc.model = self.dataSouce[indexPath.row];
    [self.navigationController pushViewController:vc animated:YES];
}


-(NSArray *)tableView:(UITableView *)tableView editActionsForRowAtIndexPath:(NSIndexPath *)indexPath{
    __weak typeof(self) weakSelf = self;
    LPSanHuiMeetingListModel *model = self.dataSouce[indexPath.row];
    UITableViewRowAction * action1 = [UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleDestructive title:@"删除" handler:^(UITableViewRowAction *action, NSIndexPath *indexPath) {
        [LPRequestManager request_miitactionShykActiondeleteShykWithMSG_ID:model.ID successBlock:^(id object) {
            NetWorkModel *work = [NetWorkModel yy_modelWithJSON:object];
            if ([work.STATE integerValue] == 20000) {
                [YJProgressHUD showSuccess:@"删除成功" inview:[UIApplication sharedApplication].keyWindow];
                dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.5 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
                    [weakSelf.dataSouce removeObjectAtIndex:indexPath.row];
                    [weakSelf.tableView reloadData];
                });
            }else{
                [YJProgressHUD showSuccess:@"删除失败" inview:[UIApplication sharedApplication].keyWindow];
            }
        } failBlock:^(id object) {
            [YJProgressHUD showSuccess:@"网络错误" inview:[UIApplication sharedApplication].keyWindow];
        }];
    }];
    
    UITableViewRowAction * action2 = [UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleDefault title:@"修改" handler:^(UITableViewRowAction *action, NSIndexPath *indexPath) {
        //Action 2
        LPSanHuiYKSaveRecordViewController *vc = [LPSanHuiYKSaveRecordViewController new];
        vc.model = model;
        vc.headline = weakSelf.headLine;
        vc.type = self.typyString;
        vc.block = ^{
            [weakSelf.tableView.mj_header beginRefreshing];
        };
        [weakSelf.navigationController pushViewController:vc animated:YES];
        
    }];
    action2.backgroundColor = [UIColor lightGrayColor];
    return @[action1,action2];
}

#pragma mark - 懒加载
/**  初始化tableView  */
- (UITableView *)tableView{
    if (!_tableView) {
        _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, navHeight, self.view.bounds.size.width, [UIScreen zj_screenHeight] - navHeight) style:UITableViewStylePlain];
        _tableView.dataSource = self;
        _tableView.delegate = self;
        _tableView.separatorColor = [UIColor clearColor];
        _tableView.tableFooterView = [UIView new];
        [_tableView registerClass:[LPSanHuiYiKeTypeDetailListTableViewCell class] forCellReuseIdentifier:CELLID];
    }
    return _tableView;
}

- (NSMutableArray *)dataSouce{
    if (!_dataSouce) {
        _dataSouce = [NSMutableArray array];
    }
    return _dataSouce;
}

- (ZJRefurbish *)refurbish{
    if (!_refurbish) {
        _refurbish = [ZJRefurbish new];
    }
    return _refurbish;
}

- (UIButton *)addButton{
    if (!_addButton) {
        _addButton = ({
            UIButton *button = [UIButton new];
            [button setTitle:@"记录" forState:UIControlStateNormal];
            [button setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
            button.backgroundColor = [UIColor themeColor];
            [button addTarget:self action:@selector(clickButton) forControlEvents:UIControlEventTouchUpInside];
            button.titleLabel.font = LYPBOLDFONT(17);
            button.layer.cornerRadius = 35 * [UIScreen zj_screenHScale];
            button.layer.masksToBounds = YES;
            button;
            
        });
    }
    return _addButton;
}


@end
